package com.jinshan.gaietymall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinshan.gaietymall.core.exception.BusinessException;
import com.jinshan.gaietymall.entity.TUser;
import com.jinshan.gaietymall.entity.request.TUserReqVO;
import com.jinshan.gaietymall.entity.request.TuerReqInsertVO;
import com.jinshan.gaietymall.entity.request.TuserQueryVO;
import com.jinshan.gaietymall.entity.response.TUserRep;
import com.jinshan.gaietymall.exception.ErrorCodesEnum;
import com.jinshan.gaietymall.mapper.TUserMapper;
import com.jinshan.gaietymall.service.ITUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liujinshan
 * @since 2020-04-15
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {


    @Autowired
    private TUserMapper tUserMapper;

    /**
      * @description 分页+条件查询用户信息
      * @author liujinshan
      * param tuserQueryVO
      * @return userPage
      * @date 2020/4/24 16:13
      */
    @Override
    public IPage<TUserRep> getAll(TuserQueryVO tuserQueryVO) {
        //参数一是当前页，参数二是每页个数
        IPage<TUser> userPage = new Page<>();
        QueryWrapper<TUser> wrapper = new QueryWrapper<TUser>();

        IPage<TUserRep> page = new Page<>();

        String username =  Optional.ofNullable(tuserQueryVO).map(TuserQueryVO::getUsername).orElse(null);
        String mobile =  Optional.ofNullable(tuserQueryVO).map(TuserQueryVO::getMobile).orElse(null);
        Integer currentPage =  Optional.ofNullable(tuserQueryVO).map(TuserQueryVO::getCurrentPage).orElse(null);
        Integer pageSize =  Optional.ofNullable(tuserQueryVO).map(TuserQueryVO::getPageSize).orElse(null);
        if (null != currentPage && null != pageSize) {
            userPage.setCurrent(tuserQueryVO.getCurrentPage());
            userPage.setSize(tuserQueryVO.getPageSize());
        }

        if (null != username && !"".equals(username)) {
            wrapper.like("username",tuserQueryVO.getUsername());
        }

        if (null != mobile && !"".equals(mobile)) {
            wrapper.like("mobile",tuserQueryVO.getMobile());
        }



        try {
            userPage = tUserMapper.selectPage(userPage,wrapper);
            BeanUtils.copyProperties(userPage,page);
        } catch (BusinessException e) {
            throw new BusinessException(ErrorCodesEnum.NONE_QUERY_DATA.code(),ErrorCodesEnum.NONE_QUERY_DATA.msg());
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }
        return page;
    }

    /**
      * @description 根据id进行查询
      * @author liujinshan
      * param tuserQueryVO
      * @return tUserRep
      * @date 2020/4/24 16:53
      */
    @Override
    public TUserRep getById(TuserQueryVO tuserQueryVO) {
        TUserRep tUserRep = new TUserRep();
        Long id = Optional.ofNullable(tuserQueryVO).map(TuserQueryVO::getId).orElse(null);
        if (null == id){
            throw new BusinessException(ErrorCodesEnum.NONE_QUERY_DATA.code(),ErrorCodesEnum.NONE_QUERY_DATA.msg());
        }
        try {
            TUser tUser =  tUserMapper.selectById(id);
            BeanUtils.copyProperties(tUser,tUserRep);
        }catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }
        return tUserRep;
    }

    /**
     * @param tUserReqVO
     * @description 根据id更新
     * @author liujinshan
     * param tUserReqVO
     * @date 2020/4/24 17:19
     */
    @Override
    public void update(TUserReqVO tUserReqVO) {
       TUser tUser = new TUser();
       try {
           BeanUtils.copyProperties(tUserReqVO,tUser);
       } catch (Exception e) {
           throw new BusinessException(ErrorCodesEnum.DATA_EXCHANGE_ERROR.code(),ErrorCodesEnum.DATA_EXCHANGE_ERROR.msg());
       }
       int i = 0;
       try {
           i = tUserMapper.updateById(tUser);
           System.out.println(i);
       } catch (Exception e) {
           throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
       }
       if(i<0){
           throw new BusinessException(ErrorCodesEnum.UPDATA_USER_DATA_ERROR.code(),ErrorCodesEnum.UPDATA_USER_DATA_ERROR.msg());
       }
    }

    /**
     * @param tUserReqVO
     * @description 根据id删除
     * @author liujinshan
     * param null tUserReqVO
     * @date 2020/4/24 17:16
     */
    @Override
    public void delete(TUserReqVO tUserReqVO) {
        int i = 0;
        try {
            i = tUserMapper.deleteById(tUserReqVO.getId());
            System.out.println(i);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }
        if(i<0){
            throw new BusinessException(ErrorCodesEnum.UPDATA_USER_DATA_ERROR.code(),ErrorCodesEnum.UPDATA_USER_DATA_ERROR.msg());
        }
    }

    /**
     * @description 新增
     * @author liujinshan
     * param tUser
     * @date 2020/4/24 17:36
     */
    @Override
    public void add(TuerReqInsertVO tUserReqVO) {
        TUser tUser = new TUser();
        try {
            BeanUtils.copyProperties(tUserReqVO,tUser);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.DATA_EXCHANGE_ERROR.code(),ErrorCodesEnum.DATA_EXCHANGE_ERROR.msg());
        }
        int i = 0;
        try {
            i = tUserMapper.insert(tUser);
            System.out.println(i);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }
        if(i<=0){
            throw new BusinessException(ErrorCodesEnum.UPDATA_USER_DATA_ERROR.code(),ErrorCodesEnum.UPDATA_USER_DATA_ERROR.msg());
        }
    }

}
